/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.bid.service.plugins  
 */
package com.apache.bid.service.plugins;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.common.BidCommonUtil;
import com.apache.bid.common.Constant;
import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.bid.common.cache.BidCacheManager;
import com.apache.bid.entity.BidItem;
import com.apache.bid.entity.BidItemHistory;
import com.apache.bid.entity.BidSession;
import com.apache.bid.manager.BidInfoBaojiaManager;
import com.apache.bid.manager.BidInfoJingjiaManager;
import com.apache.bid.manager.BidItemHistoryManager;
import com.apache.cache.util.Validator;
import com.apache.rpc.client.RpcProtoclClient;
import com.apache.tools.DateUtils;

/**
 * description:  标的恢复插件
 * @author Hou Dayu 创建时间：2016-4-13  
 * 
 */
public class UntiyRestoreItemPluginImpl implements PluginConnector {

	private Logger log = Logger.getLogger(getClass());
	private BidItemHistoryManager bidItemHistoryManager;
	private BidInfoBaojiaManager bidInfoBaojiaManager;
	private BidInfoJingjiaManager bidInfoJingjiaManager;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.zbxsoft.api.manager.PluginConnector#execute(com.zbxsoft.api.vo.ParamsVo)  
	 */
	@SuppressWarnings("rawtypes")
	public Object execute(ParamsVo param) throws Exception {
		ResultEntity ret = new ResultEntity();
		if (Constant.getInstance().ifWriteDatabase()) {
			BidItemHistory history = (BidItemHistory) bidItemHistoryManager.getInfoById(param);
			if (Validator.isEmpty(history)) {
				ret.setResult("false");
				ret.setEntity("false");
				ret.setMessage("没有获取到标的信息");
				return ret;
			}
			if (!"0".equals(history.getSessionId())) {//验证活动的状态
				BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("session");
				if (!cache.checkCacheObject(cache.session_prefix + history.getSessionId())) {
					ret.setResult("false");
					ret.setEntity("false");
					ret.setMessage("标的所在活动不具备发布标的条件，请先发布活动");
					return ret;
				}
				BidSession session = (BidSession) cache.getCacheObjectByKey(cache.session_prefix
						+ history.getSessionId());
				if (!Constant.getInstance().getSessionConfig("statusPubslish").equals(session.getStatus())) {
					ret.setResult("false");
					ret.setEntity("false");
					ret.setMessage("标的所在活动还没有发布，请先发布活动");
					return ret;
				}
			}
			String firstBidTime = String.valueOf(param.getParams("firstBidTime"));
			String openTime = String.valueOf(param.getParams("openTime"));
			String record = String.valueOf(param.getParams("record"));
			BidItem item = new BidItem();
			BeanUtils.copyProperties(history, item);
			if (Validator.isNull(openTime)) {
				item.setOpenTimePre(history.getOpenTimePre());
			} else {
				item.setOpenTimePre(openTime);
			}
			if ("F".equalsIgnoreCase(record)) {///是否删除竞价记录
				ParamsVo jiaVo = new ParamsVo();
				jiaVo.setInfoId(item.getItemId());
				jiaVo.setMethodKey("ByItemId");
				if (Constant.BIDMODE_YCXASC.equals(item.getBidMode())
						|| Constant.BIDMODE_YCXDESC.equals(item.getBidMode())) {
					bidInfoBaojiaManager.deleteInfo(jiaVo);
				} else {
					bidInfoJingjiaManager.deleteInfo(jiaVo);
				}
			}
			item.setFirstValuePre(history.getFirstValuePre());
			item.setFirstBidTimePre(firstBidTime);
			item.setCloseTimePre(setCloseTime(history.getBidMode(), history.getLimitTime(), firstBidTime));
			UnityItemPluginHelper.getInstance().restoreItem(history, item);
			bidItemHistoryManager.deleteInfo(param);
			ret.setResult("true");
			ret.setEntity("true");
			ret.setMessage("标的信息恢复成功");
		} else {
			param.setParams("infoId", param.getInfoId());
			ret = RpcProtoclClient.getInstance().doService("wbdCacheService", "restoreItem", param.getParams(),
					BidCommonUtil.getInstance().getInterfaceRegister("", ""));
			log.info("result=" + ret.getResult() + ";msg=" + ret.getMessage());
		}
		return ret;
	}

	private String setCloseTime(String bidMode, String limitTime, String firstBidTime) throws Exception {
		String colseTime = "";
		if (Constant.BIDMODE_JTZX.equals(bidMode) || Constant.BIDMODE_JTFX.equals(bidMode)) {
			colseTime = BidCommonUtil.getInstance().getMergeTime(firstBidTime, limitTime);
		} else if (Constant.BIDMODE_YCXASC.equals(bidMode) || Constant.BIDMODE_YCXDESC.equals(bidMode)) {
			colseTime = firstBidTime;
		} else if (Constant.BIDMODE_HELAN.equals(bidMode)) {
			long limittime = Long.valueOf(limitTime) * 1000 * 2;
			Date data = new Date(DateUtils.toLong(DateUtils.parse(firstBidTime, "yyyy-MM-dd HH:mm:ss")) + limittime);
			colseTime = DateUtils.format(data, "yyyy-MM-dd HH:mm:ss");
		} else if (Constant.BIDMODE_FEJJZX.equals(bidMode) || Constant.BIDMODE_FEJJFX.equals(bidMode)) {
			colseTime = BidCommonUtil.getInstance().getMergeTime(firstBidTime, limitTime);
		}
		return colseTime;
	}

	public void setBidItemHistoryManager(BidItemHistoryManager bidItemHistoryManager) {
		this.bidItemHistoryManager = bidItemHistoryManager;
	}

	public void setBidInfoBaojiaManager(BidInfoBaojiaManager bidInfoBaojiaManager) {
		this.bidInfoBaojiaManager = bidInfoBaojiaManager;
	}

	public void setBidInfoJingjiaManager(BidInfoJingjiaManager bidInfoJingjiaManager) {
		this.bidInfoJingjiaManager = bidInfoJingjiaManager;
	}

}
